package edu.cdbi.database;

import junit.framework.TestCase;
import edu.cdbi.model.instance.Course;

public class CourseManagementTest extends TestCase {
	
	private CourseManagement management;

	protected void setUp() throws Exception {
		super.setUp();
		management = new CourseManagement();
	}

	protected void tearDown() throws Exception {
		super.tearDown();
	}

	public void testInsert() {
		int id = management.insert("1", "hu");
		Course course = management.getData(id);
		System.out.println("course.getTeacherId():"+course.getTeacherId());
		assertTrue(course.getTeacherId() == 1);
		assertTrue(course.getName().equals("hu"));
		management.delete(id);
	}

	public void testGetList() {
		/*讀取2筆測試資料，測試搜尋功能是否正確
		 * 分別為
		 * 1. 2, chen
		 * 2. 3, shi
		 */
		int counter = 0;
		for(Course course : management.getList()){
			if(course.getTeacherId() == 2 &&
					course.getName().equals("chen")){
				counter ++;
			}else if(course.getTeacherId() == 3 &&
					course.getName().equals("shi")){
				counter ++;
			}
		}
		
		assertTrue(counter == 2);
	}

	public void testDelete() {
		/*刪除1筆測試資料，測試刪除功能是否正確
		 * 分別為
		 * 1. 5, 4, ten
		 */
		int counter = 0;
		for(Course course : management.getList()){
			if(course.getId() == 5){
				counter ++;
			}
		}
		
		assertTrue(counter == 1);
		
		management.delete(5);
		for(Course course : management.getList()){
			if(course.getId() == 5){
				counter --;
			}
		}
		assertTrue(counter == 1);
	}
}
